var booksCaseManager;
$(function() {

	booksCaseManager = window['g'] = $("#maingrid").ligerGrid({
		height : '100%',
		url:'/v1/bcase/getBooksCase',
		columns : [ {
			display : '编号',
			name : 'id',
			align : 'left',
			showTitle : true,
			minWidth : 100,
			align : "center",
			isSort : false
		}, {
			display : '书架名',
			name : 'name',
			minWidth : 100,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		},{
			display : '书架备注',
			name : 'remark',
			minWidth : 200,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		}],
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdatebooksCase,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : itembooksCaseClick,
				icon : 'add'
			}, {
				line : true
			}]
		},
		onSelectRow: function (rowdata, rowindex)
        {
           
        },
        enabledEdit: true, 
        isScroll: false, 
        rownumbers:true
	});
    
    jQuery("#pageloading").hide();
});
function getUpdate()
{
    var data = booksCaseManager.getUpdated();
    alert(JSON.stringify(data));
    return data;
}

function saveUpdatebooksCase(item){
	var data = booksCaseManager.getUpdated();
	var addData = booksCaseManager.getAdded();
	if(addData.length > 0){
		for(var i=0;i<addData.length;i++){
			data.push(addData[i]);
		}
	}
	if(data.length == 0){
		$.ligerDialog.warn("亲，您还未做任何修改哦!");
		return;
	}
	doSavebooksCase(data);
}
function doSavebooksCase(data,add){
	jQuery.ajax({
		url:'/v1/bcase/saveOrUpdate',
		data:{json:JSON.stringify(data)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('亲,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '亲,已经保存'+data.sucessList.length+'条记录!' });
				booksCaseManager.loadServerData();
			}else{
				tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}
function itembooksCaseClick(item) {
	booksCaseManager.addRow({ 
    },0);
}